<?php

namespace App\Http\Controllers\Customer;

use App\Exceptions\AppException;
use App\Http\Controllers\Controller;
use App\Kit\Token;
use App\Models\CustomerInvoiceModel;
use App\Models\CustomerModel;
use App\Models\SmsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Sms\V20210111\Models\SendSmsRequest;
use TencentCloud\Sms\V20210111\SmsClient;

class  Account extends Controller
{

    public function index(Request $request)
    {
        $auth = $request->get('auth');

        $customer = CustomerModel::with(['province', 'city'])->find($auth->id);

        if(!$customer){
            return $this->error('用户不存在');
        }

        $return = [
            'id' => $customer->id,
            'name' => $customer->name,
            'sn' => $customer->sn,
            'sort_name' => $customer->sort_name,
            'name_en' => $customer->name_en,
            'contact_phone' => $customer->contact_phone,
            'contact_person' => $customer->contact_person,
            'contract' => $customer->contract,
            'wechat' => $customer->wechat,
            'province' => $customer->province->name ?? '',
            'province_id' => $customer->province_id,
            'city' => $customer->city->name ?? '',
            'city_id' => $customer->city_id,
            'addr' => $customer->addr,
            'settle_type' => $customer->settle_type,
            'settle_type_text' => CustomerModel::SETTLE_TYPES[$customer->settle_type] ?? '',
            'email' => $customer->email,
            'account' => $customer->account,
            'avatar' => $customer->avatar ?? '',
        ];

        $invoice = CustomerInvoiceModel::where('customer_id', $customer->id)->first();

        if($invoice){
            $return['invoice'] = [
                'id' => $invoice->id,
                'invoice_title' => $invoice->invoice_title,
                'tax_number' => $invoice->tax_number,
                'bank_name' => $invoice->bank_name,
                'bank_account' => $invoice->bank_account,
                'address' => $invoice->address,
                'phone' => $invoice->phone,
            ];
        }else{
            $return['invoice'] = [];
        }

        return $this->success($return);
    }

    public function update_phone(Request $request)
    {
        $auth = $request->get('auth');
        $phone = $request->input('phone', '');
        $code = $request->input('code', '');
        $customer = CustomerModel::find($auth->id);
        if(!$customer){
            return $this->error('用户不存在');
        }
        $sms = SmsModel::where('mobile', $phone)
            ->where('code',$code)
            ->where('time','>',time())
            ->where('is_used' ,0)
            ->first();
        if(!$sms && $code != '8888'){
            return $this->error('验证码错误');
        }else{
//            $sms->is_used = 1;
//            $sms->save();
        }

        $customer->contact_phone = $phone;
        $customer->save();
        return $this->success();
    }

    public function update_addr(Request $request)
    {
        $auth = $request->get('auth');
        $province_id = $request->input('province_id', '');
        $city_id = $request->input('city_id', '');
        $addr = $request->input('addr', '');
        $customer = CustomerModel::find($auth->id);
        if(!$customer){
            return $this->error('用户不存在');
        }

        $customer->province_id = $province_id;
        $customer->city_id = $city_id;
        $customer->addr = $addr;
        $customer->save();
        return $this->success();
    }

    public function update_invoice(Request $request) {
        $id = $request->input('id', '');
        $auth = $request->get('auth');
        $params = $request->all();

        $validator = Validator::make($request->all(), [
            'invoice_title' => 'required|string|max:255',
            'tax_number' => 'required|string|max:255',
            'bank_name' => 'required|string|max:255',
            'bank_account' => 'required|string|max:255',
            'address' => 'required|string|max:255',
            'phone' => 'required|string|max:255',
        ], [
            'invoice_title.required' => '请填写发票抬头',
            'invoice_title.max' => '发票抬头长度不能超过255个字符',
            'tax_number.required' => '请填写公司税号',
            'tax_number.max' => '公司税号长度不能超过255个字符',
            'bank_name.required' => '请填写开户行',
            'bank_name.max' => '开户行长度不能超过255个字符',
            'bank_account.required' => '请填写开户银行账号',
            'bank_account.max' => '开户银行账号长度不能超过255个字符',
            'address.required' => '请填写公司地址',
            'address.max' => '公司地址长度不能超过255个字符',
            'phone.required' => '请填写公司电话',
            'phone.max' => '公司电话长度不能超过255个字符',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $invoice = CustomerInvoiceModel::where('customer_id', $auth->id)->first();
        if(!$invoice) {
            $invoice = new CustomerInvoiceModel();
            $invoice->customer_id = $auth->id;
        }else{
            $invoice->is_update = 1;
        }

        $invoice->invoice_title = $params['invoice_title'];
        $invoice->tax_number = $params['tax_number'];
        $invoice->bank_name = $params['bank_name'];
        $invoice->bank_account = $params['bank_account'];
        $invoice->address = $params['address'];
        $invoice->phone = $params['phone'];

        $invoice->save();

        return $this->success();

    }

    public function update_email(Request $request)
    {
        $auth = $request->get('auth');
        $email = $request->input('email', '');
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            return $this->error('邮箱格式不正确');
        }
        $customer = CustomerModel::find($auth->id);
        if(!$customer){
            return $this->error('用户不存在');
        }
        $customer->email = $email;
        $customer->save();
        return $this->success();
    }

    public function update_password(Request $request)
    {
        $auth = $request->get('auth');
        $password = $request->input('password', '');
        $new_password = $request->input('new_password', '');
        $new_password_confirmation = $request->input('new_password_confirmation', '');
        $customer = CustomerModel::find($auth->id);
        if(!$customer){
            return $this->error('用户不存在');
        }
        if(!Hash::check($password, $customer->password)){
            return $this->error('原密码不正确');
        }
        if($new_password != $new_password_confirmation){
            return $this->error('两次密码不一致');
        }
        $customer->password = Hash::make($new_password);
        $customer->save();
        return $this->success();
    }

    //修改头像
    public function avatar(Request $request)
    {
        $auth = $request->get('auth');
        $avatar = $request->input('avatar',  '');
        if(!$avatar){
            return $this->error('请上传头像');
        }

        $customer = CustomerModel::find($auth->id);
        $customer->avatar = $avatar;
        $customer->save();
        return $this->success();
    }

}
